package com.mdlib.helper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.event.EavluationEvent;
import com.mdlib.helper.model.entity.MoneyEntity;
import com.mdlib.helper.model.entity.OrderEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * 作者：HQY on 16/11/24 17:40
 * 邮箱：hqy_xz@126.com
 */

public class EvaluationRunManDialogFragment extends DialogFragment {


    @Bind(R.id.iv_evaluation_bg)
    ImageView mIvEvaluationBg;
    @Bind(R.id.tv_online_pay)
    TextView mTvOnlinePay;
    @Bind(R.id.rb_evaluation_number)
    RatingBar mRbEvaluationNumber;

    private Dialog eavluationSuccess;

    private OrderEntity order;

    public static EvaluationRunManDialogFragment newInstance(OrderEntity orderEntity) {
        EvaluationRunManDialogFragment fragment = new EvaluationRunManDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIHelper.ORDERIDINFO, orderEntity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            dismiss();
        }

        order = (OrderEntity) getArguments().getSerializable(UIHelper.ORDERIDINFO);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Share);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_evaluation_run_man, null);
        builder.setView(view);
        ButterKnife.bind(this, view);
        return builder.create();
    }


    @Override
    public void onStart() {
        super.onStart();

        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.CENTER;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick(R.id.tv_online_pay)
    public void onClick() {
        evaluationRun();
    }


    private void evaluationRun() {
        String rationg = String.valueOf(mRbEvaluationNumber.getRating());
        addSubscribe(DataManager.getInstance().getUserApi()
                .evaluateRun(order.getId() + "", rationg, "0")
                .compose(RxHelper.<MoneyEntity>defaultHandleResult())
                .subscribe(new RxSubscribe<MoneyEntity>() {
                    @Override
                    protected void onSucc(MoneyEntity money) {
                        eavluationSuccess = new Dialog(getActivity(), R.style.dialog);
                        eavluationSuccess.setContentView(R.layout.dialog_eavluation_success);

                        ImageView close = (ImageView) eavluationSuccess.findViewById(R.id.iv_dialog_evaluation_close);
                        TextView mTvTipsMoney = (TextView) eavluationSuccess.findViewById(R.id.tv_dialog_evaluation_tips_money);
                        TextView mTvMoney = (TextView) eavluationSuccess.findViewById(R.id.tv_dialog_evaluation_money);
                        mTvTipsMoney.setText(getActivity().getResources().getString(R.string.evaluation_success_tips_money, money.getMoney()));
                        mTvMoney.setText(money.getMoney());

                        close.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                EventBus.getDefault().post(new EavluationEvent("0"));
                                eavluationSuccess.dismiss();
                            }
                        });
                        eavluationSuccess.show();

                        dismiss();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }


    // rx总线
    private static CompositeSubscription compositeSubscription;

    public static void addSubscribe(Subscription subscription) {
        if (compositeSubscription == null) {
            compositeSubscription = new CompositeSubscription();
        }
        compositeSubscription.add(subscription);
    }

    public void unSubscribe() {
        if (compositeSubscription != null) {
            compositeSubscription.unsubscribe();
            compositeSubscription = null;
        }
    }

}
